package com.sdry.web.controller.zc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sdry.model.lz.Warehouse;
import com.sdry.model.zc.ZcGeneralQueryEntity;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.model.zc.ZcTrayAndLocationEntity;
import com.sdry.service.zc.ZcInventoryManagementService;
import com.sdry.service.zc.ZcUpAndDownService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcUpAndDownController   
 * @Description:  上架下架
 * @Author:       zc   
 * @CreateDate:   2019年5月23日 下午4:23:33   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("upAndDown")
public class ZcUpAndDownController {
	@Resource
	private ZcInventoryManagementService zcInventoryManagementService;
	@Resource
	private ZcUpAndDownService zcUpAndDownService;
	/**
	 * 进入上下架页面
	 * @return
	 */
	@RequestMapping("enterUpAndDownPage")
	public ModelAndView enterUpAndDownPage(){
		ZcGeneralQueryEntity zcGeneralQueryEntity = new ZcGeneralQueryEntity();
		List<Warehouse> allWarehouseList = zcInventoryManagementService.selectAllWarehouseList(zcGeneralQueryEntity);
		ModelAndView mav = new ModelAndView();
		mav.addObject("allWarehouseList",allWarehouseList);
		mav.setViewName("zc/warehouse/upAndDown");
		return mav;
	}
	/**
	 * 上架
	 * @param response
	 * @param zcTrayAndLocationEntity
	 */
	@RequestMapping("bindingTrayAndLocation")
	public void bindingTrayAndLocation(HttpServletResponse response,HttpSession httpSession,ZcTrayAndLocationEntity zcTrayAndLocationEntity){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ZcSysUserEntity su = (ZcSysUserEntity)httpSession.getAttribute("user");
		zcTrayAndLocationEntity.setBinding_person(su.getId());
		zcTrayAndLocationEntity.setBinding_date(sdf.format(date));
		int affact = zcUpAndDownService.bindingTrayAndLocation(zcTrayAndLocationEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 下架
	 * @param response
	 * @param zcTrayAndLocationEntity
	 */
	@RequestMapping("unbindTrayAndLocation")
	public void unbindTrayAndLocation(HttpServletResponse response,ZcTrayAndLocationEntity zcTrayAndLocationEntity){
		int affact = zcUpAndDownService.unbindTrayAndLocation(zcTrayAndLocationEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/****************************************************APP*****************************************************/
	/**
	 *  上架
	 * @param response
	 * @param tray_code 托盘码 多个用逗号隔开
	 * @param location_code 库位码
	 * @param binding_person 绑定人id
	 */
	@RequestMapping("bindingTrayAndLocationApp")
	public void bindingTrayAndLocationApp(HttpServletResponse response,String tray_code,String location_code,String binding_person){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ZcTrayAndLocationEntity zcTrayAndLocationEntity = new ZcTrayAndLocationEntity();
		zcTrayAndLocationEntity.setTray_code(tray_code);
		zcTrayAndLocationEntity.setLocation_code(location_code);
		zcTrayAndLocationEntity.setBinding_date(sdf.format(date));
		zcTrayAndLocationEntity.setBinding_person(Long.parseLong(binding_person));
		int affact = zcUpAndDownService.bindingTrayAndLocation(zcTrayAndLocationEntity);
		JSONObject jsonObject = new JSONObject();
		if(affact > 0){
            jsonObject.put("status", "1");
            jsonObject.put("data", "提交成功");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        } else if(affact == -2){
        	jsonObject.put("status", "0");
            jsonObject.put("message", "提交失败，请勿上架空托盘");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        } else{
        	jsonObject.put("status", "0");
            jsonObject.put("message", "提交失败");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
	/**
	 * 下架
	 * @param response
	 * @param tray_code
	 * @param location_code
	 */
	@RequestMapping("unbindTrayAndLocationApp")
	public void unbindTrayAndLocationApp(HttpServletResponse response,String tray_code,String location_code){
		ZcTrayAndLocationEntity zcTrayAndLocationEntity = new ZcTrayAndLocationEntity();
		zcTrayAndLocationEntity.setTray_code(tray_code);
		zcTrayAndLocationEntity.setLocation_code(location_code);
		int affact = zcUpAndDownService.unbindTrayAndLocation(zcTrayAndLocationEntity);
		JSONObject jsonObject = new JSONObject();
		if(affact > 0){
            jsonObject.put("status", "1");
            jsonObject.put("data", "提交成功");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        } else {
        	jsonObject.put("status", "0");
            jsonObject.put("message", "提交失败");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
}
